<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="内容" prop="operaContent">
        <el-input
            v-model="queryParams.operaContent"
            clearable
            maxlength="50"
            placeholder="请输入内容"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操控类型" prop="operaType">
        <el-select v-model="queryParams.operaType" clearable placeholder="操控类型">
          <el-option v-for="dict in operaType"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['train:log:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="ID" prop="id" width="100"/>
      <el-table-column align="center" label="用户名称" prop="userName" width="200"/>
      <el-table-column align="center" label="车辆名称" prop="carName" width="200"/>
      <el-table-column align="center" label="操作类型" prop="operaType" width="200">
        <template #default="scope">
          <el-tag style="width:80px" type="success">{{ scope.row.operaType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作时间" prop="createTime" width="200"/>
      <el-table-column align="center" how-overflow-tooltip label="操作内容" prop="operaContent"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="200">
        <template #default="scope">
          <el-button
              v-hasPermi="['train:log:remove']"
              icon="Delete"
              type="text"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />
  </div>
</template>

<script name="Log" setup>
import {delLog, listLog} from "@/api/controlLog/log";

const {proxy} = getCurrentInstance();
const {operaType} = proxy.useDict("operaType");

const logList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    operaType: null,
    operaContent: null
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询车辆操控日志列表 */
function getList() {
  loading.value = true;
  listLog(queryParams.value).then(response => {
    logList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    operaType: null,
    operaContent: null
  };
  proxy.resetForm("logRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ida = row.id || ids.value;
  proxy.$modal.confirm('是否确认删该日志数据？').then(function () {
    return delLog(ida);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>
